function [lpostd,flag_ok]= ...
    modePosteriorMin(parest,parStru,posStru,model,dataStru,prpar,prss,filterStru,flags,struIn)
% =========================================================================
%% modePosterior.m
%
% This code computes the posterior of a state space model with potentially 
% 1) NaNs in the observables 
% 2) NB Breaks, where the solution matrices have NB pages 
% 
% *No temporal aggregation* allowed  
% 
%% Inputs 
% parest:       (portion of) parameter vector being estimated
%
% parStru:      structure with
%               .param if the model is solved using a vector of parameters
%           OR  .namesEstimated if the model is solved using structures 
%
% posStru:      .param.est    rows of paramter vector estimated
%               .steady.est   rows of ss vector being estimated 
%                                   leave empty if none 
%
% model:        .handle      Needed to solve model 
%               .addsol
%               .solveOpt
%
% dataStru:     .data     [T n] vector of data
%               .trainVec [2 1] position to start and end the sample in
%                         computing the likelihood
%
% filterStru:   .tauVec   vector of indicators of which pages to use for
%                         each observation
%               .aZero    Initial state,  if flags.initialCond==1
%               .pZero    Initial VCV,    if flags.initialCond==1
%
% flags        .initialCond == 1 if the initialization of the filter is
%               given 
% 
%% Notes:  
%% Synch:  
%This function  is synched with likelNanSplitsGeneral.m 
%% Related: 
% Use modePosterior.m for standard evaluation 
% A. Justiniano Feb 20 2014 
% =======================================================================
lpostd=1e20;flag_ok=0;
%% 1. Transform to go from MIN to MOD
if flags.transform==1
    parest=feval(@min2mod,parest,struIn.transMat);
end

%% Here until part 5 should match modePosterior.m 

%% 1.a Keep this always to ensure working with latest Value
paramVec=parStru.param;
paramVec(posStru.param.est)=parest;

%% 2. Compute the Likelihood
try
    [likel,ssvec,flagOK]=feval(filterStru.funcLikel,parest,parStru,...
        posStru,model,dataStru,filterStru,flags);
catch
    disp('Error in modePosteriorOpt')
    lasterr
    flagOK=0;
    return
end
%dispaj(likel);
if flagOK==0;
    return
end
if isinf(abs(likel))==true;
    disp('Likelihood is infinite!')
    return;
end;
if isnan(likel)==true;
    disp('Likelihood is Nan!')    
    return;
end;
%% 3. Prior Density of Parameters
lpriord=sum(feval(@priordens,paramVec, prpar.prior, prpar.alphap, prpar.betap, prpar.lbnd, prpar.ubnd));
if isinf(abs(lpriord))==1;return;end
%% 4. Prior Density on Steady State
if ~isempty(posStru.steady.est)
    lpriord_ss=sum(feval(@priordens,ssvec(posStru.steady.est), prss.prior, prss.alphap, prss.betap, prss.lbnd, prss.ubnd));
    if isinf(abs(lpriord_ss))==1;return;end
else
    lpriord_ss=0;
end
%% 5. Log Posterior
%  Notice the - since optimizing and should be a minimum 
lpostd=-(lpriord + lpriord_ss + likel);
flag_ok=1;